package cn.lanqiao.ui;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import cn.lanqiao.entity.User;
import cn.lanqiao.entity.UserLogin;

public class UserLoginFrm extends JFrame {

	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private JTextField accountField;
	private JPasswordField passwordField;
	public static String  account;
	private JCheckBox remPassword;
	private static JCheckBox autoLogin;
	private static JButton btnLogin;
	
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					UserLoginFrm frame = new UserLoginFrm();
					frame.setLocationRelativeTo(null);
					File file = new File("bin\\autoLogin.dat");
					if(file.exists()) {
						autoLogin.setSelected(true);
						frame.setVisible(true);
						btnLogin.doClick();
					}else {
						frame.setVisible(true);
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	public UserLoginFrm() {
		setIconImage(Toolkit.getDefaultToolkit().getImage("photoes\\tubiao.png"));
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setResizable(false);
		setTitle("登录");
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBackground(new Color(255, 255, 255));
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel label = new JLabel("账号：");
		label.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		label.setBounds(65, 130, 49, 34);
		contentPane.add(label);
		
		JLabel label_1 = new JLabel("密码：");
		label_1.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		label_1.setBounds(65, 174, 49, 34);
		contentPane.add(label_1);
		
		accountField = new JTextField();
		accountField.setBounds(124, 139, 214, 21);
		contentPane.add(accountField);
		accountField.setColumns(10);
		
		JButton button = new JButton("注册账号");
		button.setForeground(Color.GRAY);
		button.setBackground(Color.WHITE);
		button.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				UserLoginFrm.this.setVisible(false);
				RegisterFrm Regiter = new RegisterFrm();
				Regiter.setLocationRelativeTo(null);
				Regiter.setVisible(true);
			}
		});
		button.setBounds(2, 244, 93, 23);
		contentPane.add(button);
		
		btnLogin = new JButton("登录");
		btnLogin.setIcon(new ImageIcon("photoes\\denglu.png"));
		btnLogin.setBackground(Color.WHITE);
		btnLogin.setForeground(Color.BLACK);
		btnLogin.setToolTipText("");
		btnLogin.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		btnLogin.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String uaccount = accountField.getText();
				account = accountField.getText();
				String upassword = new String(passwordField.getPassword());
				cn.lanqiao.service.UserLoginService service =  new cn.lanqiao.service.impl.UserLoginServiceImpl();
				UserLogin user = service.login(uaccount, upassword);
				if(user==null) {
					JOptionPane.showMessageDialog(null, "输入信息有误！", "信息有误", JOptionPane.YES_NO_OPTION);
				}else {
					UserLoginFrm.this.setVisible(false);
					MainFrame main = new MainFrame();
					main.setLocationRelativeTo(null);
					main.setVisible(true);
				}
				//记住密码
				boolean result =remPassword.isSelected();
				if(result) {
					File file = new File("bin//rem.properties");
					try {
						file.createNewFile();
						remPassword(uaccount,upassword);
					} catch (IOException e1) {
						e1.printStackTrace();
					}
				}else {
					File file = new File("bin//rem.properties");
					if(file.exists()) {
						file.delete();
						remPassword.setSelected(false);
					}
				}
				//自动登录
				boolean autoLoginSelect = autoLogin.isSelected();
				File file = new File("bin\\autoLogin.dat");
				if(autoLoginSelect) {
					try {
						file.createNewFile();
					} catch (IOException e1) {
						e1.printStackTrace();
					}
				}else {
					file.delete();
					autoLogin.setSelected(false);
				}
				
				
			}
		});
		btnLogin.setBounds(130, 233, 203, 33);
		contentPane.add(btnLogin);
		
		passwordField = new JPasswordField();
		passwordField.setBounds(124, 182, 214, 21);
		contentPane.add(passwordField);
		
		JLabel label_3 = new JLabel("");
		label_3.setIcon(new ImageIcon("photoes\\账号图标.png"));
		label_3.setBounds(15, 128, 35, 34);
		contentPane.add(label_3);
		
		JLabel label_4 = new JLabel("");
		label_4.setIcon(new ImageIcon("photoes\\密码图标.png"));
		label_4.setBounds(15, 167, 34, 34);
		contentPane.add(label_4);
		
		remPassword = new JCheckBox("记住密码");
		remPassword.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(remPassword.isSelected() == false) {
					autoLogin.setSelected(false);
				}
			}
		});
		remPassword.setBackground(Color.WHITE);
		remPassword.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		remPassword.setBounds(121, 209, 103, 23);
		contentPane.add(remPassword);
		
		JLabel label_5 = new JLabel("");
		label_5.setIcon(new ImageIcon("photoes\\touxiang.png"));
		label_5.setBounds(198, 77, 55, 55);
		contentPane.add(label_5);
		
		JLabel lblNewLabel = new JLabel("");
		lblNewLabel.setIcon(new ImageIcon("photoes\\tiankong.png"));
		lblNewLabel.setBounds(0, 0, 444, 106);
		contentPane.add(lblNewLabel);
		
		autoLogin = new JCheckBox("自动登录");
		autoLogin.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(autoLogin.isSelected() == true) {
					remPassword.setSelected(true);
				}
			}
		});
		autoLogin.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		autoLogin.setBackground(Color.WHITE);
		autoLogin.setBounds(260, 209, 86, 23);
		contentPane.add(autoLogin);
		
		//记住密码处理
		File file = new File("bin//rem.properties");
		if(file.exists()) {
			remPassword.setSelected(true);
			User u = getAccount();
			accountField.setText(u.getAccount());
			passwordField.setText(u.getPassword());
			}
		}
	
		//记住密码
		public void remPassword(String account,String password) {
			OutputStream out;
			try {
				out = new FileOutputStream("bin//rem.properties",false);
				Properties pro = new Properties();
				pro.setProperty("uaccount", account);
				pro.setProperty("upassword", password);
				pro.store(out, "记住密码");
			} catch (FileNotFoundException e1) {
				e1.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		//取账号密码
		public User getAccount() {
			User user = new User();
			ClassLoader loader = ClassLoader.getSystemClassLoader();
			InputStream in = loader.getResourceAsStream("rem.properties");
			Properties pro = new Properties();
			try {
				pro.load(in);
				String account = pro.getProperty("uaccount");
				String password = pro.getProperty("upassword");
				user.setAccount(account);
				user.setPassword(password);
			} catch (IOException e) {
				e.printStackTrace();
			}
			return user;
		}
		//删除记住密码
		public void deleteRemP() {
			File file = new File("bin\\rem.properties");
			if(file.exists()) {
				file.delete();
				remPassword.setSelected(false);
			}
		}
}
